import roleApi from "@/api/acl/role";
import { Message } from "element-ui";
import permissionApi from "@/api/acl/permission";


const state = {
  total:0,
  pageList:[],
  assignList:[],
  assignListIds:[],
  selIds:[]
}
const mutations = {
  SET_PAGELIST(state,data){
    state.total = data.total
    state.pageList = data.items.map(item => {
      return {
        ...item,
        showInp:false,
        editRoleName:item.roleName
      }
    })
  },
 SET_ASSIGNLIST(state,data){
  state.assignList = data
 },
 SET_ASSIGNLISTIDS(state,data){
  state.assignListIds = data
 },
 SET_SELIDS(state,data){
  state.selIds = data
 }
}
const actions = {
  // 获取分页列表
  async getPageList({commit},params){
    try {
      const res = await roleApi.getPageList(params.page, params.limit, params)
      if(res && res.code === 20000){
        commit('SET_PAGELIST',res.data)
      }
    } catch (error) {
      console.error(error);
      Message.error('获取分页列表失败')
    }
  },
  // 添加角色功能函数
  async saveRole({commit},params){
    try {
      const res = await roleApi.save(params)
      if(res && res.code === 20000){
        Message.success('添加成功')
      }
    } catch (error) {
      console.error(error);
      Message.error('添加失败')
    }
  },
  // 批量删除多个角色
  async removeRoles({commit},ids){
    try {
      const res = await roleApi.removeRoles(ids)
      if(res && res.code === 20000){
        return Promise.resolve('成功')
      }else {
        return Promise.reject('失败')
      }
    } catch (error) {
      console.error(error);
      Message.error('批量删除失败')
      return Promise.reject('失败')
    }
  },
  // 单个删除回调
  async removeById({commit},id){
    try {
      const res = await roleApi.removeById(id)
      if(res && res.code === 20000){
        Message.success('删除成功')
      }else {
        Message.error('删除失败')
      }
    } catch (error) {
      console.error(error);
      Message.error('删除失败')
    }
  },
  async updateById({commit},role){
    try {
      const res = await roleApi.updateById(role)
      if(res && res.code === 20000){
        Message.success('修改成功')
      }else {
        Message.error('修改失败')
      }
    } catch (error) {
      console.error(error);
      Message.error('修改失败')
    }
  },
  async getAssign({commit},roleId){
    try {
      const res = await roleApi.getAssign(roleId)
      if(res && res.code === 20000){

        commit('SET_ASSIGNLIST',res.data.children)

        const ids = []
        const selIds = []

        function getIds(arr){
          arr.children.forEach(item => {

            if(item.children && item.children.length === 0){

              ids.push(item.id)
              if(item.select){
                selIds.push(item.id)
              }
            }
            if(item.children){
              getIds(item)
            }

          })
        }
        getIds(res.data)

        commit('SET_ASSIGNLISTIDS',ids)
        commit('SET_SELIDS',selIds)
        
      }
    } catch (error) {
      console.error(error);
    }
  },
  async doAssign({commit},params){
    try {
      await permissionApi.doAssign(params.roleId,params.permissionId)

    } catch (error) {
      console.error(error);
    }
  }
}

export default {
  state,
  mutations,
  actions,
  namespaced:true
}